package com.longchen.mall.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xbfinal
 * @Package com.longcehng.mall.common
 * @Date 2024/7/14 10:07
 * 描述 : 统一返回类
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1807809144088741640L;
    @ApiModelProperty(value = "是否成功")
    private Boolean success;


    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "消息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private T data;

    @ApiModelProperty(value = "动态数据")
    private Map<String,Object> map = new HashMap<>();

    /**
     * 防止直接new
     */
    private Result(){};

    /**
     * 成功的静态方法
     *
     */
    public static<T> Result<T> ok( ){
        Result<T> r=new Result<>();
        r.success=true;
        r.code= ResultCode.SUCCESS;
        r.message="成功";
        return r;
    }
    public static<T> Result<T> ok( T data){
        Result<T> r=new Result<>();
        r.success=true;
        r.code= ResultCode.SUCCESS;
        r.message="成功";
        r.data = data;
        return r;
    }

    /**
     * 失败的静态方法
     */
    public static<T> Result<T> failed( ){
        Result<T> r=new Result<>();
        r.success=false;
        r.code=ResultCode.ERROR;
        r.message="失败";
        return r;
    }
    public static<T> Result<T> failed(String message ){
        Result<T> r=new Result<>();
        r.success=false;
        r.code=ResultCode.ERROR;
        r.message=message;
        return r;
    }

    public Result<T> addCode(Integer code){
        this.setCode(code);
        return this;
    }
    public Result<T> addMessage(String message){
        this.setMessage(message);
        return this;
    }
    public Result<T> addData(T data){
        this.setData(data);
        return this;
    }

    /**
     * 用来操作 map = new HashMap(); 动态数据
     * @param key
     * @param value
     * @return
     */
    public Result<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }



}